<template>
    <div class="app-container">
      <div>
        <!-- 条件栏 -->
        <PageFilter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
        >
          <template v-slot:filter-operateMonth>
            <el-date-picker
              v-model="filterInfo.query.operateMonth"
              type="month"
              :disabled-date="pickerOption.disabledDate"
              value-format="YYYY-MM-DD"
              placeholder="请选择月份"
              
            >
            </el-date-picker>
          </template>
          <!-- @change="monthChange" -->
          <!-- <template v-slot:filter-operateTime>
            <el-date-picker
              v-model="operateTime"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="至"
              :picker-options="$fn.pickerOptionsFn()"
              align="right"
              start-placeholder="操作时间开始日期"
              end-placeholder="操作时间结束日期"
              :shortcuts="$fn.pickerOptionsFn()"
              @change="timeChange"
            ></el-date-picker>
          </template> -->
        </PageFilter>
  
        <!-- 表格 -->
        <TableContainer
          :refresh="tableInfo.refresh"
          :init-curpage="tableInfo.initCurpage"
          :pageTotal="pageInfos.pageTotal"
          v-model:data="tableInfo.data"
          :tab-index="false"
          :check-box="false"
          v-model:query="filterInfo.query"
          :field-list="tableInfo.fieldList"
          :api="tableInfo.api"
          :pager="false"
          :summary="true"
          :summary-field-list="tableInfo.summaryFieldList"
          :isDynamic="false"
        >
        </TableContainer>
      </div>
    </div>
  </template>
  
  <script setup name="purchaseSalesStockOld">
  import { fatchTemplateByCode } from '@/api/base'
  import {
    selectPurchaseSalesStockOldList,
    exportPurchaseSalesStockOld,
    printPurchaseSalesStockOld,
  } from '@/api/dataQuery.js'
  const { proxy } = getCurrentInstance()
//   const operateTime = ref([])
  const filterInfo = ref({
    query: {
      operateMonth: '',
      inventoryCategories: 1,
    //   operateTimeLesser: '',
    //   operateTimeGreater: '',
    },
    list: [
      {
        type: 'select-model',
        label: '库存类别',
        value: 'inventoryCategories',
        code: 'inventory_categories',
        event: search,
      },
      {
        label: '日期',
        value: 'operateMonth',
        type: 'slot',
        event: search,
      },
    //   {
    //     label: '操作时间',
    //     value: 'operateTime',
    //     type: 'slot',
    //     event: search,
    //   },
  
      { type: 'button', label: '搜索', event: search },
      { type: 'button', label: '打印', event: print,  show: proxy.$permission('purchaseSalesStockOld:print') },
      {
        type: 'button',
        label: '导出excel',
        event: exportExcel,
        show: proxy.$permission('purchaseSalesStockOld:print'),
      },
    ],
  })
  const tableInfo = ref({
    refresh: 1,
    initCurpage: 1,
    data: [],
    api: undefined,
    selections: [],
    summaryFieldList: [
      { value: 'beforeMonthAmount', formatter: proxy.$fn.TripartiteMethod },
      { value: 'monthInAmount', formatter: proxy.$fn.TripartiteMethod },
      { value: 'monthOutAmount', formatter: proxy.$fn.TripartiteMethod },
      { value: 'currentMonthAmount', formatter: proxy.$fn.TripartiteMethod },
    ],
    fieldList: [
      { label: '类别', value: 'financialAccountValue' },
      { label: '上月库存', value: 'beforeMonthAmount', type: 'column', formatter: proxy.$fn.TripartiteMethod },
      { label: '本月购入', value: 'monthInAmount', type: 'column', formatter: proxy.$fn.TripartiteMethod },
      { label: '本月发出', value: 'monthOutAmount', type: 'column', formatter: proxy.$fn.TripartiteMethod },
      { label: '本月库存', value: 'currentMonthAmount', type: 'column', formatter: proxy.$fn.TripartiteMethod },
    ],
  })
  const pageInfos = ref({
    pageSize: 20,
    curPage: 1,
    pageTotal: 0,
  })
  const pickerOption = ref({
    disabledDate(time) {
      let nowData = new Date('2022-08-01')
      nowData = new Date(nowData.setDate(nowData.getDate() - 1))
      return time < nowData
    },
  })
  function getList() {
    tableInfo.value.api = selectPurchaseSalesStockOldList
    tableInfo.value.refresh = Math.random()
  }
  function search() {
    filterInfo.value.query.inventoryCategories =
      filterInfo.value.query.inventoryCategories &&
      parseInt(filterInfo.value.query.inventoryCategories)
    tableInfo.value.initCurpage = Math.random()
    tableInfo.value.refresh = Math.random()
  }
//   function timeChange(val) {
//     if (val && val.length) {
//       filterInfo.value.query.operateTimeGreater = val[0] + ' 00:00:00'
//       filterInfo.value.query.operateTimeLesser = val[1] + ' 23:59:59'
//       filterInfo.value.query.operateMonth = ''
//     } else {
//       filterInfo.value.query.operateTimeGreater = ''
//       filterInfo.value.query.operateTimeLesser = ''
//     }
//     search()
//   }
  function monthChange(val) {
    if (val) {
      operateTime.value = []
    //   filterInfo.value.query.operateTimeGreater = ''
    //   filterInfo.value.query.operateTimeLesser = ''
    }
  }
  //打印
  function print() {
    fatchTemplateByCode({
      resourceCode: 'purchaseSalesStock:print',
      isDefault: 1,
    }).then((res) => {
      if (res && res.success) {
        let temp = proxy.$fn.getPrintTemp(res.data[0])
        getPrintData(temp)
      }
    })
  }
  function getPrintData(temp) {
    let params = {
      ...filterInfo.value.query,
    }
    printPurchaseSalesStockOld(params).then((res) => {
      if (res.success && res) {
        let data = proxy.$fn.deepClone(res.data)
        let newData = data.map((item) => {
          return {
            ...item,
            ...item.orderInfo,
          }
        })
        proxy.$lodop.preview(temp, newData)
      }
    })
  }
  function exportExcel() {
    exportPurchaseSalesStockOld(
      proxy.$fn.delEmptyAttribute(filterInfo.value.query)
    ).then((resp) => {
      if (resp) {
        proxy.$fn.parseExcel(resp)
      }
    })
  }
  function getDate() {
    const date = new Date()
    const year = date.getFullYear()
    let month = date.getMonth() + 1
    month < 10 ? (month = '0' + month) : month
    return year + '-' + month + '-' + '01'
  }
  
  filterInfo.value.query.operateMonth = getDate()
  
  onMounted(() => {
    getList()
  })
  </script>
  
  <style scoped></style>
  